package com.java.exer_3;

/**
 * @author shkstart
 * @create 2021-01-07 18:19
 */
public class GoodsTest_1 implements Comparable {
    private String name;
    private double price;

    public GoodsTest_1() {

    }

    public GoodsTest_1(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "GoodsTest{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    //指明商品按照什么方式进行排序:按照从低到高
    @Override
    public int compareTo(Object o) {
        if (o instanceof GoodsTest_1) {
            GoodsTest_1 goodsTest1 = (GoodsTest_1) o;
            if (this.price > goodsTest1.price) {
                return 1;
            } else if (this.price < goodsTest1.price) {
                return -1;
            } else {
                this.compareTo(goodsTest1.getName());
            }

        }
        throw new RuntimeException("您输入的不是一个商品");
    }

}